package com.wondersgroup.fjzlpt.yyzyzx.util;

import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.web.wapper.QuickPrincipal;
import com.quick.framework.util.QuickPrincipalUtils;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 控制层工具类
 * <p>
 * <p>
 * Created by sanma on 2017/9/19.
 */
public class ControllerUtils  extends  SystemConstant{

    public static JSONObject getRequestData(HttpServletRequest request){
        JSONObject requestData = new JSONObject(1);
		/*是否是管理者*/
        boolean isManager = true;
        QuickPrincipal principal = (QuickPrincipal)request.getUserPrincipal();
        String WSJGLBDM = principal.getUserInfo().getString("WSJGLBDM");
        if(!StringUtils.isEmpty(WSJGLBDM) && WSJGLBDM.indexOf(WSGLJGLBDM) != 0){
            isManager = false;
            JSONObject userInfo = principal.getUserInfo();
            requestData = userInfo;
        }
        requestData.put("isManager", isManager);
        return requestData;
    }

    public static void setQueryParams(Map map , HttpServletRequest request){
        JSONObject requestData = getRequestData(request);
        if(!requestData.getBoolean("isManager")){
            map.put("YLJGDM" , requestData.get("YLJGDM"));
        }else{
            if(map.get("XZQHDM") == null || StringUtils.isEmpty(map.get("XZQHDM").toString())){
                String xzqhdm = QuickPrincipalUtils.getXzqhdm(request);
                map.put("XZQHDM" , xzqhdm);
            }
        }
    }

    /**
     * EasyUI的分页参数转换
     * @param map
     */
    public static void pageFormat(Map map){
        Object rows = map.remove("rows");
        if(rows != null){
            map.put("pageSize", rows.toString());
        }
        Object page = map.remove("page");
        if(page != null){
            map.put("pageNumber", page.toString());
        }
    }
}
